package com.sky.controller.admin;

import com.aliyuncs.ecs.model.v20140526.DescribeInvocationResultsResponse;
import com.fasterxml.jackson.databind.ser.Serializers;
import com.github.pagehelper.Page;
import com.sky.constant.JwtClaimsConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import com.sky.vo.EmployeeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@Api(tags ="员工登录")
@RequestMapping("/admin/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     * @param employeeLoginDTO
     * @return
     */
    @ApiOperation("登录")
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @ApiOperation("退出")
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }
    /**
     *
     * 添加员工
     * */
    @PostMapping
    public Result<String> add(@RequestBody EmployeeDTO employeeDTO){
        employeeService.add(employeeDTO);
        return Result.success();
    }
    /**
    *分页查询
    * */
    @GetMapping("/page")
    public Result<PageResult> page(EmployeePageQueryDTO employeePageQueryDTO){
      PageResult pageResult = employeeService.page(employeePageQueryDTO);
      return Result.success(pageResult);
    }
    /**
     * 修改状态
     */
    @PostMapping("/status/{status}")
    public Result enableOrDisable(@PathVariable Integer status, Long id ){
        Employee employee = Employee.builder()
                .status(status)
                .id(id)
                /*.updateTime(LocalDateTime.now())
                .updateUser(BaseContext.getCurrentId())*/
                .build();
        employeeService.update(employee);
        return Result.success();
    }
    /**
     * 根据id查询员工信息
     * @param id
     * @return
     * */
    @ApiOperation("根据id查询员工信息")
    @GetMapping("{id}")
    public Result<Employee> findById(@PathVariable Long id){
        Employee employee = employeeService.findById(id);
        return Result.success(employee);
    }
   @ApiOperation("根据id修改员工")
   @PutMapping
    public Result modify(@RequestBody EmployeeDTO employeeDTO){
       Employee employee = new Employee();
       BeanUtils.copyProperties(employeeDTO,employee);

       /*employee.setUpdateTime(LocalDateTime.now());
       employee.setUpdateUser(BaseContext.getCurrentId());*/

       employeeService.update(employee);
       return Result.success();
   }
   @PostMapping("/up")
    public Result upPassword(@RequestBody PasswordEditDTO passwordEditDTO){
        employeeService.upPassword(passwordEditDTO);
        return Result.success();
   }
}
